package fr.norsys.mapper.console.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.beanutils.BeanUtils;

import fr.norsys.mapper.console.exception.IdentifiantException;
import fr.norsys.mapper.console.model.IdentifiantComponent;
import fr.norsys.mapper.console.utils.UIDGenerator;

public class IdentifiantServiceImpl implements IdentifiantService {

	IdentifiantServiceImpl(){
		super();
	}
	/**
	 * Service to save an identifiant component in the list in session
	 * @param identifiant : identifiant component
	 * @param identifiants : list of identifiant components
	 */
	public void save(IdentifiantComponent identifiant, List identifiants)throws IdentifiantException {
		if(identifiant.getId().equals("")){
			add(identifiant,identifiants);
		}else{
			modify(identifiant,identifiants);
		}
	}

	/**
	 * Service to add an identifiant component in the list in session
	 * @param identifiant : identifiant component
	 * @param identifiants : list of identifiant components
	 */
	private void add(IdentifiantComponent identifiant, List identifiants)throws IdentifiantException {
		IdentifiantComponent ic = null;
		try {
			ic = (IdentifiantComponent) BeanUtils.cloneBean(identifiant);
		} catch (Exception e) {
			throw new IdentifiantException(e.toString());
		}
		ic.setId(UIDGenerator.generateId());
		getVariable(ic);
		identifiants.add(ic);
	}
	
	/**
	 * Service to modify an identifiant component in the list in session
	 * @param identifiant : identifiant component
	 * @param identifiants : list of identifiant components
	 */
	private void modify(IdentifiantComponent ident, List identifiants) {
		IdentifiantComponent identifiant = ident;
		for (Iterator it = identifiants.iterator(); it.hasNext();) {
			IdentifiantComponent ic = (IdentifiantComponent) it.next();
			if (identifiant.getId().equals(ic.getId())) {
				fr.norsys.mapper.console.utils.BeanUtils.copyFilledProperties(
						ic, identifiant);
				getVariable(ic);
				identifiant=ic;
				break;
			}
		}
	}
	
	/**
	 * Change the id value with ${] if it is a variable
	 * @param idComp
	 */
	private void getVariable(IdentifiantComponent idComp) {
		IdentifiantComponent ic = idComp;
		String value = null;
		if(ic.isIdVariable()){
			value = ic.getValue();
			value = "${"+value+"}";
			ic.setValue(value);
		}
		
	}
	/**
	 * Service to delete an identifiant component in the list in session
	 * @param identifiant : identifiant component
	 * @param identifiants : list of identifiant components
	 */
	public void delete(String id, List identifiants) {
		for (Iterator it = identifiants.iterator(); it.hasNext();) {
			IdentifiantComponent v = (IdentifiantComponent) it.next();
			if (v.getId().equals(id)) {
				identifiants.remove(v);
				break;
			}
		}
	}

	/**
	 * Action to get an identifiant component from the list
	 * @param id : id of the identifiant component
	 * @param identifiants : list of identifiant components
	 * @return
	 */
	public IdentifiantComponent get(String id, List identifiants) {
		IdentifiantComponent result = null;
		String value = null;
		for (Iterator it = identifiants.iterator(); it.hasNext();) {
			IdentifiantComponent c = (IdentifiantComponent) it.next();
			if (c.getId().equals(id)) {
				result = c;
				value= result.getValue();
				boolean isVar = result.isIdVariable();
				if(isVar){
					int idx = -1;
					idx = value.indexOf("}");
					if(idx>0){
						value=value.substring(2,idx);					
					}
				}
				result.setValue(value);
				break;
			}
		}
		return result;
	}

	/**
	 * Action to list the identifiants components of the complete 
	 * identifiant of the resource.
	 * @param currentId : the complete identifiant of the resource.
	 * @return
	 */
	public List list(String currentId) {
		List ids = new ArrayList();
		String id;
		int idx;
		String name;
		String value;
		boolean isIdVariable;
		IdentifiantComponent identifiant = null;
		StringTokenizer st = new StringTokenizer(currentId, ",");
		while(st.hasMoreTokens()){
			isIdVariable = false;
			id=st.nextToken();
			idx = id.indexOf("=");
			if(idx>0){
				name=id.substring(0,idx);
				value=id.substring(idx+1);
				idx=value.indexOf("{");
				if(idx>-1) {
					isIdVariable = true;
				}
				identifiant = new IdentifiantComponent(name, value, isIdVariable);
				ids.add(identifiant);
			}
		}
		
		return ids;
	}

	/**
	 * Service to get the complete identifiant, from the list of components
	 * @param identifiants
	 * @return
	 */
	public String getCompleteId(List identifiants) {
		StringBuffer completeId = new StringBuffer();
		for(Iterator i=identifiants.iterator();i.hasNext();){
			IdentifiantComponent idComp = (IdentifiantComponent)i.next();
			String name = idComp.getName();
			String value = idComp.getValue();
			completeId.append(name).append("=");
			completeId.append(value);
			if(i.hasNext()){
				completeId.append(",");
			}
		}
		return completeId.toString();
	}

}
